﻿using AutoMapper;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Helper;
using SIG.InputModel.Admin.Store;
using SIG.Model.Store;
using SIG.Resources.Admin;
using SIG.Service;
using SIG.ViewModel;
using SIG.ViewModel.Admin.Store;
using SIG.WebMVC.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;

namespace SIG.WebMVC.Areas.Admin.Controllers
{
    [SIGAuth]
    public class GoodsPhotoController : BaseController
    {
     
        private readonly IGoodsService _goodsService;
        private readonly IGoodsPhotoService _goodsPhotoService;
        private readonly IPageMetaService _pageMetaService;
        private readonly IMapper _mapper;

        public GoodsPhotoController(IGoodsService goodsService, IGoodsPhotoService goodsPhotoService, IPageMetaService pageMetaService, IMapper mapper)
        {          
            _goodsService = goodsService;
            _goodsPhotoService = goodsPhotoService;
            _pageMetaService = pageMetaService;
            _mapper = mapper;

        }


        // GET: Admin/GoodsPhoto
        public ActionResult Index(int goodsId)
        {

            var photo = new GoodsPhotoIM
            {
                GoodsId = goodsId
            };
            var photos = _goodsPhotoService.GetPhotos(goodsId);
            ViewBag.Photos = _mapper.Map<IEnumerable<GoodsPhoto>, IEnumerable<GoodsPhotoVM>>(photos) ;
            return View(photo);
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add(GoodsPhotoIM vm)
        {
          //  AR.Data = photo.ProductId;
            //photo.AddedDate = DateTime.Now;
            //photo.AddedBy = Site.CurrentUserName;
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            var imgPath = HostingEnvironment.MapPath(vm.ImageURL);

            string fileExtension = Path.GetExtension(imgPath).ToLower();
            string thumbName = Path.GetFileNameWithoutExtension(imgPath) + "_thumb" + fileExtension;

            string thumbPath = vm.ImageURL.Substring(0, vm.ImageURL.LastIndexOf("/")) + "/" + thumbName;
            string thumbSavePath = imgPath.Substring(0, imgPath.LastIndexOf("\\")) + "\\" + thumbName;

            ImageHandler.MakeThumbnail(imgPath, thumbSavePath , SettingsManager.Store.PhotoWidth, SettingsManager.Store.PhotoHeight, "Cut", fileExtension);

            var photo = new GoodsPhoto{
                GoodsId = vm.GoodsId,
                ImageURL = vm.ImageURL,
                Thumbnail = thumbPath
            };
            
            _goodsPhotoService.Create(photo);

            var Photos = _goodsPhotoService.GetPhotos(vm.GoodsId);
            var photoList = _mapper.Map<IEnumerable<GoodsPhoto>, IEnumerable<GoodsPhotoVM>>(Photos);

            AR.Data = RenderPartialViewToString("_Photos", photoList);

            AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, EntityNames.ProductPhoto));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Delete(int id)
        {
            var photo = _goodsPhotoService.GetById(id);
            AR.Data = photo.GoodsId;
            if (photo == null)
            {
                AR.Setfailure(Messages.HttpNotFound);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            _goodsPhotoService.Delete(photo);
            AR.SetSuccess(String.Format(Messages.AlertDeleteSuccess, EntityNames.ProductPhoto));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }
    }
}